package edu.qqcui.study.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;

/**
 * 系统是Windows的成立条件，此时创建Bill的Person实例
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021-06-26 13:54
 **/
public class WindowsCondition implements Condition {
	/**
	 * Determine if the condition matches.
	 *
	 * @param context  the condition context
	 * @param metadata the metadata of the {@link AnnotationMetadata class}
	 *                 or {@link MethodMetadata method} being checked
	 * @return {@code true} if the condition matches and the component can be registered,
	 * or {@code false} to veto the annotated component's registration
	 */
	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		// 获取类加载器的信息
		ClassLoader classLoader = context.getClassLoader();
		// 获取环境信息
		Environment environment = context.getEnvironment();
		// 获取资源加载信息
		ResourceLoader resourceLoader = context.getResourceLoader();
		// 获取Bean工厂信息
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		// 获取注册器信息
		BeanDefinitionRegistry registry = context.getRegistry();

		String osName = environment.getProperty("os.name");

		if (osName.contains("Windows")) {
			return true;
		}
		return false;
	}
}
